package dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import dao.*;
import pojo.User;
import util.C3P0Util;

public class UserDaoImpl implements UserDao {
	private static QueryRunner qr = new QueryRunner(C3P0Util.getC3P0Util().getDataSource());
	
	@Override
	public int saveUser(User user) {
		//查找指定用户
		if(getAppointUser(user.getUname()) != null) {
			System.out.println("用户名重复！");
			return 0;
		}
		//遍历所有用户
/*		List<User> users = getAllUser();
		for (User u : users) {
			if(user.getUname().equals(u.getUname())) {
				System.out.println("用户名重复！");
				return 0;
			}
		}*/
		if (user.getUpic() == null) {
			user.setUpic("public.png"); // 没有上传照片时，设置默认照片：public.png
		}
		String insert_sql = "insert into userinfo(uname,upwd,uphone,uemail,umessage,upic) values(?,?,?,?,?,?)";
		Object[] params = new Object[] {user.getUname(),user.getUpwd(),user.getUphone(),user.getUemail(),user.getUmessage(),user.getUpic()};
		try {
//			System.out.println(user.getUpic());
			qr.update(insert_sql, params);
			return 1;
		} catch (SQLException e) {
			e.printStackTrace();
			return 2;
		}
	}

	@Override
	public int updateUserPwd(User user, String oldPwd, String newPwd) {
		if(user.getUpwd().equals(oldPwd)) {
			String updatePwd_sql="update userinfo set upwd=? where uname=?";
			Object[] params = new Object[] {newPwd,user.getUname()};
			try {
				qr.update(updatePwd_sql, params);
				System.out.println("修改密码成功");
				return 1;
			} catch (SQLException e) {
				e.printStackTrace();
				return 2;
			}
		}else {
			System.out.println("旧密码输入错误！无法修改密码");
			return 0;
		}
	}

	@Override
	public int updateUserMess(User user, String uemail, String uphone, String umessage) {
		String updateMess_sql="update userinfo set uemail=?,uphone=?,umessage=? where uname=?";
		Object[] params = new Object[] {uemail, uphone, umessage, user.getUname()};
		try {
			qr.update(updateMess_sql, params);
			System.out.println("更新信息成功");
			return 1;
		} catch (SQLException e) {
			System.out.println("更新信息失败");
			e.printStackTrace();
			return 0;
		}
	}
	
	@Override
	public int updateUserPic(User user, String upic) {
		String updatePic_sql="update userinfo set upic=? where uname=?";
		Object[] params = new Object[] {upic,user.getUname()};
		try {
			qr.update(updatePic_sql, params);
			System.out.println("更新照片成功");
			return 1;
		} catch (SQLException e) {
			System.out.println("更新照片失败");
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public User getAppointUser(String uname) {
		String selectAppoint_sql="select * from userinfo where uname=?";
		try {
			return qr.query(selectAppoint_sql,new BeanHandler<User>(User.class),uname);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<User> getAllUser() {
		String selectAll_sql="select * from userinfo";
		try {
			return qr.query(selectAll_sql,new BeanListHandler<User>(User.class));
		} catch (SQLException e) {	
			e.printStackTrace();
			return null;
		}
	}
	
	@Override
	public List<User> getUserByMessage(String keyword) {	
		//用contains函数匹配
/*		List<User> users = getAllUser();
		List<User> newUsers = new ArrayList<User>();
		for (User user : users) {		
			if(user.getUmessage().contains(keyword)) {
				newUsers.add(user);
			}
		}
		return newUsers;*/
		//用模糊查找语句查询
		String likeSelect = "select * from userinfo where umessage like '%"+keyword+"%'";
		try {
			return qr.query(likeSelect,new BeanListHandler<User>(User.class));
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) throws SQLException {
//		UserDaoImpl u = new UserDaoImpl();
		System.out.println(C3P0Util.getC3P0Util().getDataSource().getConnection());
//		u.saveUser(new User("张三","ss1","","","",""));
//		System.out.println(u.getAppointUser("张三").getUpwd());
//		u.updateUserPwd(u.getAppointUser("张三"), "1234", "12345");
//		List<User> users = u.getUserByMessage("动物");
//		for (User user : users) {
//			System.out.println(user.getUmessage());
//		}
	}

}
